package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.*;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 厂家、经销商、修理厂认证管理 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/sys/authentication")
@Api(description = "认证管理管理")
public class AuthenticationController extends BaseController<IZslAuthenticationService, ZslAuthentication> {

    @Autowired
    private IBaseAreaService iBaseAreaService;
    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private CompanyContactService contactService;

    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;
    @Autowired
    private CompanyAphorismService companyAphorismService;
    @Autowired
    private CommondityStatisticsService commondityStatisticsService;

    @Autowired
    private IZslStoreService iZslStoreService;

    @Autowired
    private OnlineIMService onlineIMService;

    @ApiOperation("提交认证信息")
    @RequestMapping(value = "/saveAuthentication", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> saveAuthentication(@ApiParam("企业名称") @RequestParam String companyName,
                                                              @ApiParam("联系人") @RequestParam String linkMan,
                                                              @ApiParam("电话") @RequestParam String phone,
                                                              @ApiParam("成立时间/成立日期") @RequestParam String establishmentDate,
                                                              @ApiParam("注册时间") @RequestParam String registrationDate,
                                                              @ApiParam("经营省id") Long businessProvinceId,
                                                              @ApiParam("经营市id") Long businessCityId,
                                                              @ApiParam("经营区id") Long businessDistrictId,
                                                              @ApiParam("经营地址") @RequestParam String businessAddress,
                                                              @ApiParam("经营范围") @RequestParam String businessScope,
                                                              @ApiParam("注册省id") Long registerProvinceId,
                                                              @ApiParam("注册市id") Long registerCityId,
                                                              @ApiParam("注册区id") Long registerDistrictId,
                                                              @ApiParam("注册地址") @RequestParam String registrationAddress,
                                                              @ApiParam("注册资本") @RequestParam String registeredCapital,
                                                              @ApiParam("注册号") @RequestParam String registeredNumber,
                                                              @ApiParam("法定代表人") @RequestParam String legalRepresentative,
                                                              @ApiParam("登记机关") @RequestParam String registrationAuthority,
                                                              @ApiParam("企业类型 0、有限责任公司 1、无限责任公司 2、个体经营 3、股份公司 4、两合公司") @RequestParam Integer enterpriseType,
                                                              @ApiParam("营业期限") @RequestParam String businessTerm,
                                                              @ApiParam("经营模式 0、生产厂家 1、经销批发 2、代理 3、其他") @RequestParam Integer businessModel,
                                                              @ApiParam("年检时间") @RequestParam String annualInspectionTime,
                                                              @ApiParam("购买时间") @RequestParam Integer buyYears,
                                                              @ApiParam("类型：0" +
                                                                      "、普通提交 " +
                                                                      "1、企业认证") @RequestParam Integer type,
                                                              @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {

        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        ZslAuthentication info = baseService.selectOne(new EntityWrapper<ZslAuthentication>().eq("user_id",
                sysUser.getId()));
        if (info == null) {
            info = new ZslAuthentication();
        }
        info.setCompanyName(companyName);
        info.setLinkMan(linkMan);
        info.setPhone(phone);
        info.setEstablishmentDate(establishmentDate);
        info.setRegistrationDate(registrationDate);
        info.setBusinessProvinceId(businessProvinceId);
        info.setBusinessCityId(businessCityId);
        info.setBusinessDistrictId(businessDistrictId);
        String businessArea =
                iBaseAreaService.selectById(businessProvinceId).getName() + iBaseAreaService.selectById(businessCityId).getName() + iBaseAreaService.selectById(businessDistrictId).getName();
        info.setBusinessArea(businessArea);
        info.setBusinessAddress(businessAddress);
        info.setBusinessScope(businessScope);
        info.setRegisterProvinceId(registerProvinceId);
        info.setRegisterCityId(registerCityId);
        info.setRegisterDistrictId(registerDistrictId);
        String registerArea =
                iBaseAreaService.selectById(registerProvinceId).getName() + iBaseAreaService.selectById(registerCityId).getName() + iBaseAreaService.selectById(registerDistrictId).getName();
        info.setRegisterArea(registerArea);
        info.setRegistrationAddress(registrationAddress);
        info.setRegisteredCapital(registeredCapital);
        info.setRegisteredNumber(registeredNumber);
        info.setLegalRepresentative(legalRepresentative);
        info.setRegistrationAuthority(registrationAuthority);
        info.setEnterpriseType(enterpriseType);
        info.setBusinessTerm(businessTerm);
        info.setBusinessModel(businessModel);
        info.setAnnualInspectionTime(annualInspectionTime);
        info.setCrtTime(new Date());
        info.setBuyYears(buyYears);
        info.setUserId(BaseContextHandler.getUserID());
        info.setStoreId(sysUser.getStoreId());
        if (type == 1) {
            info.setState(1);
            sysUser.setState(1);
        }

        if (info.getId() == null) {
            baseService.insert(info);
        } else {
            baseService.updateById(info);
        }
        sysUser.setStoreName(companyName);
        iSysUserService.updateById(sysUser);

        //新增或者修改公司名称，对店铺名称进行统一修改
        ZslStore zslStore = iZslStoreService.selectById(sysUser.getStoreId());
        zslStore.setName(companyName);
        iZslStoreService.updateById(zslStore);


        return new ReturnObject<>(0, "提交成功", info);
    }

    @ApiOperation("获取当前用户的认证信息")
    @RequestMapping(value = "/findMyAuthentication", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> findMyAuthentication() {
        List<ZslAuthentication> list = baseService.selectList(new EntityWrapper<ZslAuthentication>().eq("user_id",
                BaseContextHandler.getUserID()));
//        if (list.size() > 0) {
//            list.get(0).setProvince(iBaseAreaService.selectById(list.get(0).getProvinceId()).getName());
//            list.get(0).setCity(iBaseAreaService.selectById(list.get(0).getCityId()).getName());
//            list.get(0).setDistrict(iBaseAreaService.selectById(list.get(0).getDistrictId()).getName());
//            return new ReturnObject<>(0, "获取成功", list.get(0));
//        }
        return new ReturnObject<>(0, "获取成功", list.get(0));

//        return new ReturnObject<>(1, "未申请认证信息", null);
    }


    @ApiOperation("获取全部用户审核分页")
    @RequestMapping(value = "/getAuthenticationPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslAuthentication>> getAuthenticationPage(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("企业名称") @RequestParam String name,
            @ApiParam("状态 1 待审核 2 审核成功 3 审核失败") @RequestParam Integer state) {
        EntityWrapper<ZslAuthentication> ew = new EntityWrapper<>();
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        if (state != null) {
            ew.eq("state", state);
        } else {

            ew.in("state", list);
        }
        if (StringUtils.isNotBlank(name)) {

            ew.like("company_name", name).or().like("link_man", name).or().like("phone", name);
        }
        ew.orderBy("crt_time", false);
        Page<ZslAuthentication> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("查看认证详情")
    @RequestMapping(value = "/findAuthenticationInfoById", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> findAuthenticationInfoById(@ApiParam("id") @RequestParam Long id) {
        ZslAuthentication info = baseService.selectById(id);
//        info.setProvince(iBaseAreaService.selectById(info.getProvinceId()).getName());
//        info.setCity(iBaseAreaService.selectById(info.getCityId()).getName());
//        info.setDistrict(iBaseAreaService.selectById(info.getDistrictId()).getName());
        return new ReturnObject<>(0, "获取成功", info);
    }


    @ApiOperation("查看本人认证状态")
    @RequestMapping(value = "/myAuthentication", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> myAuthentication() {
        ZslAuthentication info = baseService.selectOne(new EntityWrapper<ZslAuthentication>().eq("user_id",
                BaseContextHandler.getUserID()));
//        if (info != null) {
//            info.setProvince(iBaseAreaService.selectById(info.getProvinceId()).getName());
//            info.setCity(iBaseAreaService.selectById(info.getCityId()).getName());
//            info.setDistrict(iBaseAreaService.selectById(info.getDistrictId()).getName());
//        }
        return new ReturnObject<>(0, "获取成功", info);
    }


    @ApiOperation("提交审核结果")
    @RequestMapping(value = "/authentication", method = RequestMethod.POST)
    public ReturnObject<Object> authentication(@ApiParam("id") @RequestParam Long id,
                                               @ApiParam("状态 2 审核成功 3 审核失败") @RequestParam Integer state,
                                               @ApiParam("审核原因") @RequestParam String content) {
        ZslAuthentication info = baseService.selectById(id);
        if (info != null) {
            if (state == 2) {
                info.setCertificationSucessDate(new Date());
                CommondityStatistics commondityStatistics = new CommondityStatistics();
                commondityStatistics.setUserId(info.getUserId());
                commondityStatistics.setStoreId(info.getStoreId());
                commondityStatisticsService.insert(commondityStatistics);

            }
            info.setState(state);
            baseService.updateById(info);
            SysUser sysUser = iSysUserService.selectById(info.getUserId());
//            sysUser.setFlat(Integer.valueOf()info.getBusinessModel());
            sysUser.setType(2);
            sysUser.setCompanyName(info.getCompanyName());
            sysUser.setAuthenticationId(info.getId());
            sysUser.setMarketingNum(20);
            sysUser.setState(state);
            if (state == 2) {
                try {
                    sysUser.setEndTime(changeYear(info.getBuyYears()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            iSysUserService.updateById(sysUser);

            //申请企业信息的用户
            SysUser toUser = iSysUserService.selectById(info.getUserId());
            //系统管理员
            SysUser fromSysUser = iSysUserService.selectById(BaseContextHandler.getUserID());

            OnlineIM onlineIM = new OnlineIM();
            onlineIM.setType(4);
            onlineIM.setName(fromSysUser.getUsername());
            onlineIM.setFromUserId(fromSysUser.getId());
            onlineIM.setHeadPic(fromSysUser.getHeadPortraits());
            onlineIM.setPhone(fromSysUser.getPhone());
            if (state == 2) {
                onlineIM.setContent("企业信息审核提醒：您的企业信息审核成功！审核原因：" + content);
            } else {
                onlineIM.setContent("企业审核提醒：您企业信息审核失败,请检测后重新申请！审核原因：" + content);
            }
            onlineIM.setIsDeal(1);
            onlineIM.setStoreId(toUser.getStoreId());
            onlineIM.setUserId(info.getUserId());
            ZslStore store = iZslStoreService.selectById(toUser.getStoreId());
            onlineIM.setStoreName(store.getName());
            onlineIMService.insert(onlineIM);
            return new ReturnObject<>(0, "审核提交成功", null);
        }
        return new ReturnObject<>(1, "审核内容不存在", null);
    }


    @IgnoreAuth
    @ApiOperation("获取该店铺的档案管理")
    @RequestMapping(value = "/findInfoByStoreId", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> findInfoByStoreId(@ApiParam("店铺id") @RequestParam Long storeId) {
        List<ZslAuthentication> list = baseService.selectList(new EntityWrapper<ZslAuthentication>().eq("store_id",
                storeId));
//        if (list.size() > 0) {
//            list.get(0).setProvince(iBaseAreaService.selectById(list.get(0).getProvinceId()).getName());
//            list.get(0).setCity(iBaseAreaService.selectById(list.get(0).getCityId()).getName());
//            list.get(0).setDistrict(iBaseAreaService.selectById(list.get(0).getDistrictId()).getName());
//            return new ReturnObject<>(0, "获取成功", list.get(0));
//        }
        return new ReturnObject<>(0, "获取成功", list.get(0));

//        return new ReturnObject<>(1, "未申请认证信息", null);
    }

    @ApiOperation("是否具有发布产品条件 0、具有 1、不具有")
    @RequestMapping(value = "/isAbilityRelease", method = RequestMethod.POST)
    public ReturnObject<Integer> isAbilityRelease() {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        CompanyContact companyContact = contactService.selectOne(new EntityWrapper<CompanyContact>().eq("store_id",
                sysUser.getStoreId()));
        ZslAuthentication zslAuthentication = baseService.selectOne(new EntityWrapper<ZslAuthentication>().eq(
                "store_id", sysUser.getStoreId()));
        int flag = 0;
        if (companyContact != null && zslAuthentication != null) {
            flag = 0;
        } else {
            flag = 1;
        }
        return new ReturnObject<>(0, "获取成功", flag);
    }


    @ApiOperation("获取找公司分页")
    @IgnoreAuth
    @RequestMapping(value = "/getCompanysPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslAuthentication>> getBeforeCompanysPage(@ApiParam("每页条数") @RequestParam(defaultValue =
            "10") int limit,
                                                                       @ApiParam("页数") @RequestParam(defaultValue =
                                                                               "1") int offset,
                                                                       @ApiParam("通用查询条件") @RequestParam String name,
                                                                       @ApiParam("经营模式 0、生产厂家 1、经销批发 2、代理 3、其他") @RequestParam Integer businessModel,
                                                                       @ApiParam("企业类型 0、有限责任公司 1、无限责任公司 2、个体经营 " +
                                                                               "3、股份公司 4、两合公司") @RequestParam() Integer enterpriseType,
                                                                       @ApiParam("排序(crt_time:默认)") @RequestParam(defaultValue = "crt_time") String sort,
                                                                       @ApiParam("排序方式(是否正序)") @RequestParam(defaultValue = "false") Boolean isAsc) {

        EntityWrapper<ZslAuthentication> ew = new EntityWrapper<>();
        ew.eq("state", 2);
        if (enterpriseType != null) {
            ew.eq("enterprise_type", enterpriseType);
        }
        if (businessModel != null) {
            ew.eq("business_model", businessModel);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("company_name", name).or().like("business_address", name).or().like(
                    "registration_address", name).or().like(
                    "register_area", name).or().like(
                    "business_area", name);
        }
        ew.orderBy(sort, isAsc);
        Page<ZslAuthentication> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        for (ZslAuthentication zslAuthentication : obj.getRecords()) {
            List<ZslStoreCommodity> commodities =
                    iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq("store_id",
                            zslAuthentication.getStoreId()).eq("state", 0).eq("status", 0));
            for (ZslStoreCommodity zslStoreCommodity : commodities) {
                if (zslStoreCommodity.getPicUrl() != null) {

                    String[] urls = zslStoreCommodity.getPicUrl().split(",");
                    if (urls != null) {
                        zslStoreCommodity.setPicUrl(urls[0]);
                    }
                }
            }
            zslAuthentication.setZslStoreCommoditieList(commodities);
            CompanyAphorism companyAphorism =
                    companyAphorismService.selectOne(new EntityWrapper<CompanyAphorism>().eq("store_id",
                            zslAuthentication.getStoreId()));
            if (companyAphorism != null) {

                zslAuthentication.setAphorismContent(companyAphorism.getAphorismContent());
            } else {
                zslAuthentication.setAphorismContent("");
            }
        }


        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取所有公司的经营地址列表")
    @RequestMapping(value = "/getAllAera", method = RequestMethod.POST)
    @IgnoreAuth
    public ReturnObject<List<String>> getAllAera() {
        List<String> list = new ArrayList<>();
        EntityWrapper<ZslAuthentication> ew = new EntityWrapper<>();
        ew.eq("state", 2);
        ew.orderBy("crt_time", false);
        List<ZslAuthentication> zslAuthenticationList = baseService.selectList(ew);
        if (zslAuthenticationList != null && zslAuthenticationList.size() > 0) {
            for (int i = 0; i < zslAuthenticationList.size(); i++) {
                list.add(zslAuthenticationList.get(i).getBusinessArea());
            }
        }
        return new ReturnObject<>(0, "获取成功", list);
    }


    @ApiOperation("通过userId查看企业信息")
    @RequestMapping(value = "/findInfoByUserId", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> findInfoByUserId(@ApiParam("userId") @RequestParam Long id) {
        ZslAuthentication info = baseService.selectOne(new EntityWrapper<ZslAuthentication>().eq("user_id", id));
        return new ReturnObject<>(0, "获取成功", info);
    }


    @ApiOperation("后台会员设置提交企业档案")
    @RequestMapping(value = "/saveAuth", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> saveAuth(@ApiParam("企业名称") @RequestParam String companyName,
                                                    @ApiParam("联系人") @RequestParam String linkMan,
                                                    @ApiParam("电话") @RequestParam String phone,
                                                    @ApiParam("成立时间/成立日期") @RequestParam String establishmentDate,
                                                    @ApiParam("注册时间") @RequestParam String registrationDate,
                                                    @ApiParam("经营省id") Long businessProvinceId,
                                                    @ApiParam("经营市id") Long businessCityId,
                                                    @ApiParam("经营区id") Long businessDistrictId,
                                                    @ApiParam("经营地址") @RequestParam String businessAddress,
                                                    @ApiParam("经营范围") @RequestParam String businessScope,
                                                    @ApiParam("注册省id") Long registerProvinceId,
                                                    @ApiParam("注册市id") Long registerCityId,
                                                    @ApiParam("注册区id") Long registerDistrictId,
                                                    @ApiParam("注册地址") @RequestParam String registrationAddress,
                                                    @ApiParam("注册资本") @RequestParam String registeredCapital,
                                                    @ApiParam("注册号") @RequestParam String registeredNumber,
                                                    @ApiParam("法定代表人") @RequestParam String legalRepresentative,
                                                    @ApiParam("登记机关") @RequestParam String registrationAuthority,
                                                    @ApiParam("企业类型 0、有限责任公司 1、无限责任公司 2、个体经营 3、股份公司 4、两合公司") @RequestParam Integer enterpriseType,
                                                    @ApiParam("营业期限") @RequestParam String businessTerm,
                                                    @ApiParam("经营模式 0、生产厂家 1、经销批发 2、代理 3、其他") @RequestParam Integer businessModel,
                                                    @ApiParam("年检时间") @RequestParam String annualInspectionTime,
                                                    @ApiParam("购买时间") @RequestParam Integer buyYears,
                                                    @ApiParam("类型：2、后台认证") @RequestParam Integer type,
                                                    @ApiParam("用户id") @RequestParam Long userId) {

        SysUser sysUser = iSysUserService.selectById(userId);
        ZslAuthentication info = baseService.selectOne(new EntityWrapper<ZslAuthentication>().eq("user_id",
                userId));
        if (info == null) {
            info = new ZslAuthentication();
        }
        info.setCompanyName(companyName);
        info.setLinkMan(linkMan);
        info.setPhone(phone);
        info.setEstablishmentDate(establishmentDate);
        info.setRegistrationDate(registrationDate);
        info.setBusinessProvinceId(businessProvinceId);
        info.setBusinessCityId(businessCityId);
        info.setBusinessDistrictId(businessDistrictId);
        String businessArea =
                iBaseAreaService.selectById(businessProvinceId).getName() + iBaseAreaService.selectById(businessCityId).getName() + iBaseAreaService.selectById(businessDistrictId).getName();
        info.setBusinessArea(businessArea);
        info.setBusinessAddress(businessAddress);
        info.setBusinessScope(businessScope);
        info.setRegisterProvinceId(registerProvinceId);
        info.setRegisterCityId(registerCityId);
        info.setRegisterDistrictId(registerDistrictId);
        String registerArea =
                iBaseAreaService.selectById(registerProvinceId).getName() + iBaseAreaService.selectById(registerCityId).getName() + iBaseAreaService.selectById(registerDistrictId).getName();
        info.setRegisterArea(registerArea);
        info.setRegistrationAddress(registrationAddress);
        info.setRegisteredCapital(registeredCapital);
        info.setRegisteredNumber(registeredNumber);
        info.setLegalRepresentative(legalRepresentative);
        info.setRegistrationAuthority(registrationAuthority);
        info.setEnterpriseType(enterpriseType);
        info.setBusinessTerm(businessTerm);
        info.setBusinessModel(businessModel);
        info.setAnnualInspectionTime(annualInspectionTime);
        info.setCrtTime(new Date());
        info.setBuyYears(buyYears);
        info.setUserId(userId);
        info.setStoreId(sysUser.getStoreId());
        info.setCertificationSucessDate(new Date());
        info.setState(2);
        sysUser.setState(2);
        try {
            sysUser.setEndTime(changeYear(buyYears));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (info.getId() == null) {
            baseService.insert(info);
        } else {
            baseService.updateById(info);
        }
        sysUser.setStoreName(companyName);
        iSysUserService.updateById(sysUser);

        //新增或者修改公司名称，对店铺名称进行统一修改
        ZslStore zslStore = iZslStoreService.selectById(sysUser.getStoreId());
        zslStore.setName(companyName);
        iZslStoreService.updateById(zslStore);

        //申请企业信息的用户
        SysUser toUser = iSysUserService.selectById(info.getUserId());
        //系统管理员
        SysUser fromSysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OnlineIM onlineIM = new OnlineIM();
        onlineIM.setType(4);
        onlineIM.setName(fromSysUser.getUsername());
        onlineIM.setFromUserId(fromSysUser.getId());
        onlineIM.setHeadPic(fromSysUser.getHeadPortraits());
        onlineIM.setPhone(fromSysUser.getPhone());
        onlineIM.setContent("企业信息审核提醒：您的企业信息审核成功！");
        onlineIM.setIsDeal(1);
        onlineIM.setStoreId(toUser.getStoreId());
        onlineIM.setUserId(info.getUserId());
        ZslStore store = iZslStoreService.selectById(toUser.getStoreId());
        onlineIM.setStoreName(store.getName());
        onlineIMService.insert(onlineIM);
        return new ReturnObject<>(0, "提交成功", info);
    }

    @ApiOperation("后台修改企业档案")
    @RequestMapping(value = "/updateAuth", method = RequestMethod.POST)
    public ReturnObject<ZslAuthentication> updateAuth(@ApiParam("企业名称") @RequestParam String companyName,
                                                      @ApiParam("联系人") @RequestParam String linkMan,
                                                      @ApiParam("电话") @RequestParam String phone,
                                                      @ApiParam("成立时间/成立日期") @RequestParam String establishmentDate,
                                                      @ApiParam("注册时间") @RequestParam String registrationDate,
                                                      @ApiParam("经营省id") Long businessProvinceId,
                                                      @ApiParam("经营市id") Long businessCityId,
                                                      @ApiParam("经营区id") Long businessDistrictId,
                                                      @ApiParam("经营地址") @RequestParam String businessAddress,
                                                      @ApiParam("经营范围") @RequestParam String businessScope,
                                                      @ApiParam("注册省id") Long registerProvinceId,
                                                      @ApiParam("注册市id") Long registerCityId,
                                                      @ApiParam("注册区id") Long registerDistrictId,
                                                      @ApiParam("注册地址") @RequestParam String registrationAddress,
                                                      @ApiParam("注册资本") @RequestParam String registeredCapital,
                                                      @ApiParam("注册号") @RequestParam String registeredNumber,
                                                      @ApiParam("法定代表人") @RequestParam String legalRepresentative,
                                                      @ApiParam("登记机关") @RequestParam String registrationAuthority,
                                                      @ApiParam("企业类型 0、有限责任公司 1、无限责任公司 2、个体经营 3、股份公司 4、两合公司") @RequestParam Integer enterpriseType,
                                                      @ApiParam("营业期限") @RequestParam String businessTerm,
                                                      @ApiParam("经营模式 0、生产厂家 1、经销批发 2、代理 3、其他") @RequestParam Integer businessModel,
                                                      @ApiParam("年检时间") @RequestParam String annualInspectionTime,
                                                      @ApiParam("购买时间") @RequestParam Integer buyYears,
                                                      @ApiParam("类型：2、后台认证") @RequestParam Integer type,
                                                      @ApiParam("用户id") @RequestParam Long id) {

        ZslAuthentication info =
                baseService.selectById(id);
        SysUser sysUser = iSysUserService.selectById(info.getUserId());
        if (info == null) {
            info = new ZslAuthentication();
        }
        info.setCompanyName(companyName);
        info.setLinkMan(linkMan);
        info.setPhone(phone);
        info.setEstablishmentDate(establishmentDate);
        info.setRegistrationDate(registrationDate);
        info.setBusinessProvinceId(businessProvinceId);
        info.setBusinessCityId(businessCityId);
        info.setBusinessDistrictId(businessDistrictId);
        String businessArea =
                iBaseAreaService.selectById(businessProvinceId).getName() + iBaseAreaService.selectById(businessCityId).getName() + iBaseAreaService.selectById(businessDistrictId).getName();
        info.setBusinessArea(businessArea);
        info.setBusinessAddress(businessAddress);
        info.setBusinessScope(businessScope);
        info.setRegisterProvinceId(registerProvinceId);
        info.setRegisterCityId(registerCityId);
        info.setRegisterDistrictId(registerDistrictId);
        String registerArea =
                iBaseAreaService.selectById(registerProvinceId).getName() + iBaseAreaService.selectById(registerCityId).getName() + iBaseAreaService.selectById(registerDistrictId).getName();
        info.setRegisterArea(registerArea);
        info.setRegistrationAddress(registrationAddress);
        info.setRegisteredCapital(registeredCapital);
        info.setRegisteredNumber(registeredNumber);
        info.setLegalRepresentative(legalRepresentative);
        info.setRegistrationAuthority(registrationAuthority);
        info.setEnterpriseType(enterpriseType);
        info.setBusinessTerm(businessTerm);
        info.setBusinessModel(businessModel);
        info.setAnnualInspectionTime(annualInspectionTime);
        info.setCrtTime(new Date());
        info.setBuyYears(buyYears);
        info.setUserId(info.getUserId());
        info.setStoreId(sysUser.getStoreId());
        info.setCertificationSucessDate(new Date());
        info.setState(2);
        sysUser.setState(2);
        try {
            sysUser.setEndTime(changeYear(buyYears));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (info.getId() == null) {
            baseService.insert(info);
        } else {
            baseService.updateById(info);
        }
        sysUser.setStoreName(companyName);
        iSysUserService.updateById(sysUser);

        //新增或者修改公司名称，对店铺名称进行统一修改
        ZslStore zslStore = iZslStoreService.selectById(sysUser.getStoreId());
        zslStore.setName(companyName);
        iZslStoreService.updateById(zslStore);

        //申请企业信息的用户
        SysUser toUser = iSysUserService.selectById(info.getUserId());
        //系统管理员
        SysUser fromSysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        OnlineIM onlineIM = new OnlineIM();
        onlineIM.setType(4);
        onlineIM.setName(fromSysUser.getUsername());
        onlineIM.setFromUserId(fromSysUser.getId());
        onlineIM.setHeadPic(fromSysUser.getHeadPortraits());
        onlineIM.setPhone(fromSysUser.getPhone());
        onlineIM.setContent("企业信息审核提醒：您的企业信息修改成功！");
        onlineIM.setIsDeal(1);
        onlineIM.setStoreId(toUser.getStoreId());
        onlineIM.setUserId(info.getUserId());
        ZslStore store = iZslStoreService.selectById(toUser.getStoreId());
        onlineIM.setStoreName(store.getName());
        onlineIMService.insert(onlineIM);
        return new ReturnObject<>(0, "提交成功", info);
    }

    public Date changeYear(Integer buyYears) throws Exception {
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdfMM = new SimpleDateFormat("MM");
        SimpleDateFormat sdfDD = new SimpleDateFormat("dd");
        String currentDate = sdfDD.format(new Date());
        String currentYear = sdfYear.format(new Date());
        String currentMM = sdfMM.format(new Date());
        if (buyYears == 1) {
            currentYear = String.valueOf(Integer.valueOf(currentYear) + 1);
        } else if (buyYears == 2) {
            currentYear = String.valueOf(Integer.valueOf(currentYear) + 2);
        } else {
            currentYear = String.valueOf(Integer.valueOf(currentYear) + 3);
        }
        String lastDate = currentYear + "-" + currentMM + "-" + currentDate;

        return ymd.parse(lastDate);
    }


}